/***********************************************************************//**
 *   \file FFT.cpp		\author belot nicolas (karrakis)           
 *   \brief define FFT
 *    a class for Handling FFT		                                   *
 ************************************************************************//*
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "FFT.h"
#include <iostream>
namespace libtrckr {
FFT::FFT():size(0){
	in=0;
	out=0;
	forward=0;
	backward=0;
}

FFT::FFT(unsigned int Size):size(Size){
	in=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);
	out=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);
	forward=fftw_plan_dft_1d(size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
	backward=fftw_plan_dft_1d(size, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
}

FFT::FFT(const FFT &f):size(f.size){
	*this=f;
}

FFT& FFT::operator=(const FFT& f){
	if(this==&f) return *this;
	if(in!=0)	fftw_free(in);
	if(out!=0)	fftw_free(out);
	if(forward!=0) fftw_destroy_plan(forward);
	if(backward!=0) fftw_destroy_plan(backward);
	size=f.size;
	in=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);
	out=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);
	forward=fftw_plan_dft_1d(size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
	backward=fftw_plan_dft_1d(size, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
	memcpy(in, f.in, sizeof(fftw_complex)*size);
	memcpy(out, f.out, sizeof(fftw_complex)*size);
	return *this;
}

FFT::~FFT(){
	std::cout << "Désallocation de la FFT...." << std::endl;
	if(backward!=0)
		fftw_destroy_plan(backward);
	if(forward!=0)
		fftw_destroy_plan(forward);
	if(in!=0)
		fftw_free(out);
	if(out!=0)
		fftw_free(in);
}

void FFT::setIn(double *data){
	for(unsigned int i=0;i<size;i++){
		in[i][0]=data[i];
		in[i][1]=0.;
	}
}

void FFT::setIn(const Buffer &data){
	for(unsigned int i=0;i<size;i++){
		in[i][0]=data[i];
		in[i][1]=0.;
	}
}

void FFT::setIn(const DFT &data){
	double *re=data.getRealParts();
	double *im=data.getImaginaryParts();
	for(unsigned int i=0;i<size;i++){
		in[i][0]=re[i];
		in[i][1]=im[i];
	}
	free(re);
	free(im);
}

void FFT::setIn(fftw_complex *data){
	memcpy(in,data, sizeof(fftw_complex)*size);
	/*for(unsigned int i=0;i<size;i++){
		in[i][0]=data[i][0];
		in[i][1]=data[i][1];
	}*/
}

DFT FFT::execute(){
	fftw_execute(forward);
	return DFT(out,size);
}

DFT FFT::inverse(){
	fftw_execute(backward);
	return DFT(out,size);
}

unsigned int FFT::Size() const{
	return size;

}

void FFT::Size(unsigned int size){
	if(in!=0)
		fftw_free(in);
	in=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);
	if(out!=0)
		fftw_free(out);
	out=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);
	if(forward!=0)
		fftw_destroy_plan(forward);
	forward=fftw_plan_dft_1d(size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
	if(backward!=0)
		fftw_destroy_plan(backward);
	backward=fftw_plan_dft_1d(size, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
}


}

